﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClientAppViaWebService.BaseLogicServiceRef;
using ClientAppViaWebService.Utility;

namespace ClientAppViaWebService.Forms
{
    public partial class InsertForm : Form
    {
        protected BaseLogicServiceRef.BaseLogicService baseLogicService;
        private TObject parentTObject;       

        public InsertForm()
        {
            InitializeComponent();
        }

        public InsertForm(TObject parent)
        {
            InitializeComponent();
            baseLogicService = new BaseLogicService();
            parentTObject = parent;
            LoadAvailbleDtaBases();
        }     

        private void LoadRullInsertClassesByDBandParentClassId()
        {
            cmbClasses.ValueMember = "ID";
            cmbClasses.DisplayMember = "NAME";
            cmbClasses.Text = String.Empty;
            cmbClasses.DataSource = baseLogicService.SelectChildRuleInsertClassesByParentClass_Id
                      (cmbDatabases.SelectedValue.ToString(), parentTObject.CLASS_ID);
        
        }

        private void LoadAvailbleDtaBases()
        {           
            cmbDatabases.DataSource = baseLogicService.SelectAvailableDataBases();
            if (cmbDatabases.Items.Count > 0) cmbDatabases.SelectedIndex = 0;
        }

         
        private void cmbDatabases_SelectedIndexChanged(object sender, EventArgs e)
        {
             LoadRullInsertClassesByDBandParentClassId();
        }
        
        private void btnInsertEntity_Click(object sender, EventArgs e)
        {   
            //TObjectEditForm form = new TObjectEditForm();

            //    form.SetForm(parentTObject, 
            //    ClientAppViaWebService.Forms.TObjectEditForm.eMode.mdAdd, 
            //    cmbDatabases.SelectedText);
           
            //form.ShowDialog();

            TObjectEditForm form = ReflectionLogic.CreateFormByTObjectClassId(cmbDatabases.SelectedValue.ToString(),
              
                int.Parse( cmbClasses.SelectedValue.ToString()));
              // parentTObject.CLASS_ID);
              //2);
                form.SetForm(parentTObject, 
                ClientAppViaWebService.Forms.TObjectEditForm.eMode.mdAdd, 
                cmbDatabases.SelectedValue.ToString());

                form.ShowDialog();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();           
        }
    }
}
